﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cstdx.Proto.Std;

/// <summary>
/// 证券数量请求工具类
/// </summary>
public static class SecurityCountHelper
{
	/// <summary>
	/// 创建证券数量请求包
	/// </summary>
	/// <param name="market">市场类型</param>
	/// <returns>证券数量请求包实例</returns>
	public static SecurityCountRequest NewSecurityCountRequest(Exchange.MarketType market)
	{
		// 转换16进制字符串为字节数组（对应Go的HexString2Bytes）
		byte[] unknown1 = HexStringToBytes("0c 0c 18 6c 00 01 08 00 08 00 4e 04");
		byte[] unknown2 = HexStringToBytes("75 c7 33 01");

		return new SecurityCountRequest
		{
			Unknown1 = unknown1,
			Market = market,
			Unknown2 = unknown2
		};
	}

	/// <summary>
	/// 创建证券数量请求和响应实例
	/// </summary>
	/// <param name="market">市场类型</param>
	/// <returns>请求实例、响应实例和可能的错误</returns>
	public static (SecurityCountRequest request, SecurityCountResponse response, Exception error) NewSecurityCount(Exchange.MarketType market)
	{
		try
		{
			var request = NewSecurityCountRequest(market);
			var response = new SecurityCountResponse();
			return (request, response, null);
		}
		catch (Exception ex)
		{
			return (null, null, ex);
		}
	}

	/// <summary>
	/// 16进制字符串转字节数组（复用转换逻辑）
	/// </summary>
	private static byte[] HexStringToBytes(string hexString)
	{
		hexString = hexString.Replace(" ", "");

		if (hexString.Length % 2 != 0)
		{
			throw new ArgumentException("无效的16进制字符串", nameof(hexString));
		}

		byte[] bytes = new byte[hexString.Length / 2];
		for (int i = 0; i < bytes.Length; i++)
		{
			string hex = hexString.Substring(i * 2, 2);
			bytes[i] = Convert.ToByte(hex, 16);
		}

		return bytes;
	}
}


